import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text,Picker,Input } from '@tarojs/components'
import '../../assets/common.scss'
import CommonJs from '../../utils/common'
import '../join/join.scss'

import lbicon from '../mall/images/lbicon.png'
import lbicon2 from '../mall/images/lbicon2.png'


export default class Index extends Component {
  constructor(props) {
    super(props);
    this.state = {
       shoplist:[],
       shoparr:[],
       shopindex:0,
       type:1,//商品类型:1-现金商品 2-uc通豆商品 3-wuc积分商品
       sjtype:'现金商品',
       appmark:CommonJs.getmark(),
       price:'',
       goodsinfo:''
    }
  } 
  config = {
    navigationBarTitleText:'上架云仓商品'
  }
	
  componentWillMount () { 
    this.getshoplist();
    this.getgoodxq();
  }
  componentDidShow () {
    
  }
  componentDidUpdate () {
  }
  getshoplist(){
  	var url='/shop/papi/v1.9/shops/accountId/list'
  	var _this=this;
  	CommonJs.ajaxRequest(url,'','GET',function(res){
  		var arr=[]
  		var list=res.data;
	  	for(var i=0;i<list.length;i++){
	  		arr.push(list[i].name)
	  	}
  		_this.setState({
			  shoplist:list,
			  shoparr:arr
			})
  	},'wfw')
  }
  getgoodxq(){
  	var url='/yunwarehouse/papi/v1.9/yun-inventory/'+this.$router.params.id
  	var _this=this;
	  CommonJs.ajaxRequest(url,'','GET',function(ret){
			_this.setState({
			  goodsinfo:ret.data
			})
		},'wfw')
  }
  chooseshop(e){
  	var index=e.detail.value;
  	this.setState({
			shopindex:index
		})
  }
  handleChange(name,e){

  	let value=e.target.value;
  	this.setState({
      [name]: value
    });
  }
  onchange() {
    var _this = this;
    var arr=["WGM商品",'WUC商品','UC商品'];
    if(this.state.appmark!='xjzb'){
    	arr=["现金商品",'积分商品'];
    }
    Taro.showActionSheet({
      itemList:arr,
      success: function(res) {
        if (res.tapIndex == 0) {
          var type = 1;
          
        } 
        else if(res.tapIndex ==1){
        	var type =2
        }
        else if(res.tapIndex ==2){
          var type = 3;
        
        }
        var sjtype = arr[res.tapIndex];
        _this.setState({
          type: type,
          sjtype: sjtype,
        });
      },
    });
  }
  sure(){
  	var url='/yunwarehouse/papi/v1.9/yun-inventory/inventoryItemSku'
		var price=this.state.price;
		
		if(!price){
			CommonJs.msg('请输入金额');
	        return false;   
		}
		var dates={
		  "yunInventoryId":this.$router.params.id,
		  "price":price*100,
		  "shopId": this.state.shoplist[this.state.shopindex].id,
		  "type":this.state.type
		}
		var _this=this;
	  	CommonJs.ajaxRequest(url,dates,'POST',function(ret){
			CommonJs.msg('上架成功,跳转中')
			setTimeout(() => { 
				CommonJs.open('/pages/xijia/gxc')	
			}, 2000)
		},'wfw')
  }
  render () {
    const  shoplist=this.state.shoplist
    const  shopindex=this.state.shopindex
    const goodsinfo=this.state.goodsinfo
    return (
    	<View className='page'>
    		<View className='unpaid-content' style="background-color:#fff;border-bottom:10px solid #eee">
          <View className='unpaid-goods'>
						<View className='unpaid-pic'>
							<Image mode='scaleToFill' className='unpaid-pic-img' src={goodsinfo.itemPicture?goodsinfo.itemPicture:''}></Image>
		        </View>
						<View className='unpaid-right'>
		          <View className='unpaid-goodsname'>{goodsinfo.itemName}</View>
		          <View className='unpaid-specifications'>规格：{goodsinfo.itemSku}</View>
		          <View className='unpaid-total'>
		            <View className='unpaid-num'>数量：{goodsinfo.amount}</View>
		            <View className='unpaid-price'>{goodsinfo.price/100}</View>
		          </View>
		        </View>
		      </View>
        </View>
    		
        <View className='yslist' style="padding-bottom:20px;">
        	<Picker mode='selector'  range={this.state.shoparr}  onChange={this.chooseshop.bind(this)}>
          <View className='page-shop'>
                <View className='shop-type'>
                <Text>选择门店</Text>
                </View>
                
                <View className='shop-types'>
	               		{shoplist[shopindex].name}
                </View>
          </View>
					</Picker>
           <View className='page-shop' onClick={this.onchange.bind(this)}>
                <View className='shop-type'>
                <Text>上架类型</Text>
                </View>
                <View className='shop-types'>
                  {this.state.sjtype}
                </View>
          </View>


          <View className='page-shop'>
	          <View className='shop-type'>
	              <Text>金额</Text>
	          </View>
	          <View className='shop-input'>
	            <Input type='text' placeholder='输入上架金额'  onInput={this.handleChange.bind(this,'price')}/>
	          </View>
          </View>


         

      

        <View className='shop-btn'>
            <Button className='shopbtn'  onClick={this.sure.bind(this)}>确认上架</Button>
        </View>
			  </View>
			  </View>
       
    )
  }
}
